/**
 * artical
 */
define(function(require) {
	var comm = require('sdk/server');
	require('sdk/common');
	var artp = JSON.parse(appcan.ls.val('crossParam'));
	if(!artp){
		return console.warn('no param for news/detail');
	}
	var url;
	var shareTitle;
	var baseParam = {
		sid:appcfg.project.sid
	};
	
	var sdkID = appcan.ls.val('websiteConfig');
	if(sdkID){
		sdkID = JSON.parse(sdkID);
	}else{
		return appcan.window.openToast('缺少websiteConfig信息',2000,5);
	};
	var wxappid = sdkID.weixinAppid;

	
	var dataTemp = $('#artDetailTemp').val();
	var dataRender = function(data) {
		// alert(data.data);
		var render = etpl.compile(dataTemp);
		
		var data = data.data[0];
		shareTitle = data.name;
		$('#headTitle').text(shareTitle);
		//处理栏目数据
		if(!data['content'] && data['params']){
			var _p = JSON.parse(data['params']);
			if(_p['content']){
				data['content'] = _p['content']
			}else{
				data['content'] = '暂无数据'
			}
		};
		//console.log(data)
		var html = render({
			art: data
		});

		$('#artDetail').html(html).cacheImg();
		appcan.loading.hide();
	};
	var getData = function() {
		appcan.loading.show('',{
			wrap:'#mainCont'
		});
		appcan.ajax({
			url: appcfg.host.control + url,
			data: baseParam,
			success: function(res) {
				if (res.status === 'Y') {
					// console.log(res)
					dataRender(res);
				} else if(res.status==='N'){
					appcan.loading.hide();
					$.box.msg(res.message, {
						color: 'danger',
						delay: 2000
					});
				}
			}
		});
	};
	$('#share').on('click', function() {
		require.async('actionSheet', function(actionSheet) {
			actionSheet({
				titleText: '分享到',
				buttons: [{
					text: '微信',
					icon: '&#xe85b;'
				}, {
					text: '微博',
					icon: '&#xe85c;'
				}],
				cancelText: '取消',
				buttonClicked: function($target) {
					var txt = $.trim($target.text());
					switch (txt) {
						case '微信':
							var json = {
								thumbImg: 'http://121.42.146.193:8081/app/release/client-ico.png',
								wedpageUrl: 'http://www.jerei.com',
								scene: 1,
								title: shareTitle || '智慧重汽'
							}
							try{
								uexWeiXin.shareLinkContent(JSON.stringify(json));
							}catch(e){
								alert(e.message)
							}
							
							break;
						case '微博':

							break;
					}
					txt = null;
					return true;
				}
			});
		});

	});
	
	appcan.ready(function() {
		appcan.window.disableBounce();
		//获取内容
		if($.isPlainObject(artp)){
			if (artp.title) {
				$('#headTitle').text(artp.title);
			}
			if(artp.url && artp.id && artp.method){
				url = artp.url;
				baseParam.id = artp.id;
				baseParam.method = artp.method;
				getData();
			}else{
				appcan.window.openToast('新闻参数缺失！',2000,5);
			}
		}else{
			//默认获取新闻
			url = '/core/service/app/wcm/cms/news/control.jsp';
			baseParam.id = artp;
			baseParam.method = 'getNewsDetail';
			getData();
		}

		if(window.uexWeiXin){
			//注册微信
			uexWeiXin.registerApp(wxappid);
			//微信分享回调
			uexWeiXin.cbShareLinkContent = function(data) {
				alert(data);
			};
		}
		if(window.uexSina){
			//debug
			return
			uexSina.registerApp(sinaAppKey,sinaAppSecret,sinaRegisterURL);
			uexSina.cbRegisterApp = function(opId,dataType,data){
				if(data){
					alert('注册uexSina失败')
				}
			}
			var content = "这是来自智慧重汽APP的分享支持测试";
            var realImgPath = "http://121.42.146.193:8081/app/release/client-ico.png";
            uexSina.sendImageContent(realImgPath, content);
            exSina.cbShare = function(opId,dataType,data){
            	if(data==0){
            		appcan.window.openToast('分享成功',1000,5);
            	}
            }
		}
		
	});
});